﻿using SD.Common;
using SD.Infrastructure.EntityBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SlamDunk.MES.Domain.Entities.ResourceContext
{
    /// <summary>
    /// 设备
    /// </summary>
    public class Equipment : AggregateRootEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected Equipment()
        {
            //初始化导航属性
            this.RelatedClasses = new HashSet<EquipmentRelatedClass>();
        }
        #endregion

        #region 01.创建设备构造器
        /// <summary>
        /// 创建设备构造器
        /// </summary>
        /// <param name="equipmentNo">设备编号</param>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public Equipment(string equipmentNo, string equipmentName, Guid? facilityId, string description)
            : this()
        {
            base.Number = equipmentNo;
            base.Name = equipmentName;
            this.FacilityId = facilityId;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #endregion

        #region # 属性

        #region 工厂模型Id —— Guid? FacilityId
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        public Guid? FacilityId { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 只读属性 - 所属设备类Id集 —— IEnumerable<Guid> RelatedClassIds
        /// <summary>
        /// 只读属性 - 所属设备类Id集
        /// </summary>
        public IEnumerable<Guid> RelatedClassIds
        {
            get { return this.RelatedClasses.Select(x => x.EquipmentClassId); }
        }
        #endregion

        #region 导航属性 - 设备相关设备类集 —— ICollection<EquipmentRelatedClass> RelatedClasses
        /// <summary>
        /// 导航属性 - 设备相关设备类集
        /// </summary>
        public virtual ICollection<EquipmentRelatedClass> RelatedClasses { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改设备 —— void UpdateInfo(string equipmentNo, string equipmentName...
        /// <summary>
        /// 修改设备
        /// </summary>
        /// <param name="equipmentNo">设备编号</param>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void UpdateInfo(string equipmentNo, string equipmentName, Guid? facilityId, string description)
        {
            base.Number = equipmentNo;
            base.Name = equipmentName;
            this.FacilityId = facilityId;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #region 关联设备类 —— void RelateClasses(IEnumerable<Guid> equipmentClassIds)
        /// <summary>
        /// 关联设备类
        /// </summary>
        /// <param name="equipmentClassIds">设备类Id集</param>
        public void RelateClasses(IEnumerable<Guid> equipmentClassIds)
        {
            equipmentClassIds = equipmentClassIds?.Distinct().ToArray() ?? new Guid[0];

            this.ClearRelatedClasses();
            if (equipmentClassIds.Any())
            {
                EquipmentRelatedClass[] relatedClasses = equipmentClassIds.Select(x => new EquipmentRelatedClass(x)).ToArray();
                this.RelatedClasses.AddRange(relatedClasses);
                foreach (EquipmentRelatedClass relatedClass in relatedClasses)
                {
                    relatedClass.Equipment = this;
                }
            }
        }
        #endregion

        #region 清空设备相关设备类 —— void ClearRelatedClasses()
        /// <summary>
        /// 清空设备相关设备类
        /// </summary>
        public void ClearRelatedClasses()
        {
            foreach (EquipmentRelatedClass relatedClass in this.RelatedClasses.ToArray())
            {
                this.RelatedClasses.Remove(relatedClass);
            }
        }
        #endregion

        #region 初始化关键字 —— void InitKeywords()
        /// <summary>
        /// 初始化关键字
        /// </summary>
        private void InitKeywords()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.Number);
            builder.Append(base.Name);
            builder.Append(this.Description);

            base.SetKeywords(builder.ToString());
        }
        #endregion

        #endregion
    }
}
